﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Data;
using MYNAMESPACE;
namespace final_project
{
    public partial class ATM_Withdrow : Form
    {
        private connect_db con;
        public string nm2;
        public string passvalue
        {

            get
            {
                return nm2;
            }
            set
            {
                nm2 = value;
            }

        }
        public string nm3;
        public string passvalue1
        {

            get
            {
                return nm3;
            }
            set
            {
                nm3 = value;
            }
        }

        public ATM_Withdrow()
        {
            InitializeComponent();
        }

        private void ATM_Withdrow_Load(object sender, EventArgs e)
        {
            
            ID.Text= nm2;
           Balance.Text = nm3;
        }

        private void button2_Click(object sender, EventArgs e)
        {

            this.Hide();
            ATM_Account AT = new ATM_Account();
            AT.passvalue = ID.Text;
           
            AT.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Validating Start code

            if (this.ValidateChildren(ValidationConstraints.Enabled))
            {
                MessageBox.Show("All controls are valid!");
                //Logic to save...
            }
            else
            {
                MessageBox.Show("There are invalid controls on the form.");
                //Return user to form...
            }

            //Validating end code 

            
            user us = new user();
            
            if (How_Much.Text.Length == 0)
            {
                this.Hide();
                MessageBox.Show("must have value");
                ATM_Account Ac2 = new ATM_Account();
                Ac2.passvalue = ID.Text;
                Ac2.ShowDialog();

            }
            else

            {

                us.setnational_id(Int32.Parse(ID.Text));
                us.setvalue(double.Parse(How_Much.Text));
                us.setbalance(double.Parse(Balance.Text));
                
                    if (us.ATM_withdrwal(double.Parse(How_Much.Text)))
                    {


                        con = new connect_db();
                        con.sqlquery("update userdata set balance = (@namep1) where id= (@namep) ");
                        con.cmd.Parameters.Add("@namep", us.getnational_id());
                        con.cmd.Parameters.Add("@namep1", us.getbalance());
                        con.sqlnonex();
                        con.sqlquery("select balance from userdata where id=(@namep)");
                        con.cmd.Parameters.Add("@namep", us.getnational_id());
                        foreach (DataRow dr in con.QueryEx().Rows)
                        {

                            MessageBox.Show("the balance is " + dr[0].ToString());


                        }
                        this.Hide();
                        ATM_Account Ac = new ATM_Account();
                        Ac.passvalue = ID.Text;
                        Ac.ShowDialog();
                    }
                         else
                    {
                        MessageBox.Show("maximum value is Quarter your balance or you don't have this value ");
                    }
                }
                    //     else
                    //{
                    //    MessageBox.Show("your balance do not have this value");

                    //}
                
                   


        
                

            
        }

        private void How_Much_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(How_Much.Text))
            {

                e.Cancel = true;
                How_Much.Focus();
                errorProvider1.SetError(How_Much, "Salary Must Be Entered !");

            }
            else if (!Regex.IsMatch(How_Much.Text, @"^\d+$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(How_Much, "No Negative Numbers");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(How_Much, "");
            }
        }
    }
}
